package com.gec.controller;

import com.gec.domain.PageBean;
import com.gec.domain.PageParam;
import com.gec.domain.Role;
import com.gec.service.abst.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/*
 *  Type: Class
 *  Author: DawnFz.com
 *  Date: 2022/10/25 13:02
 *  Project: HotelSSM
 *  Version: 1.0
 *  Describe: [RoleController类]
 */
@Controller
@RequestMapping("/Role")
public class RoleController extends BaseController {
    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }


    @RequestMapping("/viewList")
    public String viewList() {
        return "role/list";
    }

    @ResponseBody
    @RequestMapping(value = "/jsonList", produces = JSON)
    public String jsonList(HttpServletRequest req) {
        PageParam param = PageParam.getPageParam(req);
        PageBean<Role> pBean;
        try {
            pBean = roleService.getRoleList(param);
            return pBean.toJSON();
        } catch (Exception e) {
            e.printStackTrace();
            return sendFailed(e);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getRole", produces = JSON)
    public String getRole(@RequestParam("id") String id) {
        try {
            Role role = roleService.getRoleById(id);
            return sendData(role);
        } catch (Exception e) {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/saveRole", produces = JSON)
    public String saveRole(HttpServletRequest req) {
        Role role = getBean(req, Role.class);
        try {
            roleService.saveRole(role);
            return sendSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delRole", produces = JSON)
    public String delRole(@RequestParam("id") String id) {
        try {
            roleService.delRole(id);
            return sendData("角色 [" + id + "] 删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }

    @RequestMapping("/viewAdd")
    public String viewAddRole() {
        return "role/addRole";
    }


    @RequestMapping(value = "/addRole", produces = JSON)
    @ResponseBody
    public String addRole(HttpServletRequest req) {
        try {
            Role role = getBean(req, Role.class);
            roleService.addRole(role);
            return sendSuccess();
        } catch (RuntimeException e) {
            return sendError("类型输入错误");
        } catch (Exception e) {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }

    @RequestMapping("/viewAddPermission")
    public String viewAddPermission() {
        return "role/addRolePermission";
    }


    @RequestMapping(value = "/addRolePermission", produces = JSON)
    @ResponseBody
    public String addRolePermission(@RequestParam("roleId") String roleId,
                                    @RequestParam("permissions") String[] permission) {
        try {

            boolean b = roleService.addRolePermission(roleId, permission);
            return sendData("添加权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return sendError(e.getMessage());
        }
    }

}
